/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.lyqg.biz;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.lyqg.Config;
import com.lyqg.dao.UserDao;
import com.lyqg.domain.OperatePoint;
import com.lyqg.domain.User;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-8-14 下午09:00:35
 */
@Service
@Transactional
public class UserService extends BaseService implements IUserService {
	@Resource
	UserDao userDao;

	@Override
	public int updatePassword(User user) {
		String hql = "UPDATE User SET password=? WHERE id=?";
		return userDao.batchExecute(hql, user.getPassword(), user.getId());
	}

	@Override
	public int updateUsername(User user) {
		String hql = "UPDATE User SET username=? WHERE id=?";
		return userDao.batchExecute(hql, user.getUsername(), user.getId());
	}

	@Override
	public int updateUsernameAndPassword(User user) {
		String hql = "UPDATE User SET username=?,password=? WHERE id=?";
		return userDao.batchExecute(hql, user.getUsername(),
				user.getPassword(), user.getId());
	}

	@Override
	public void save(User user) {
		userDao.save(user);
	}

	@Override
	public User findUniqueBy(String propertyName, Object value) {
		return userDao.findUniqueBy(propertyName, value);
	}

	@Override
	public User getUserById(int uid) {
		return userDao.get(uid);
	}

	@Override
	public User login(User user) {
		User dbUser = null;
		dbUser = this.findUniqueBy("username", user.getUsername());
		if (dbUser != null) {
			if (user.getPassword().equals(dbUser.getPassword())) {
				return dbUser;
			}
		}
		dbUser = this.findUniqueBy("email", user.getUsername());
		if (dbUser != null) {
			if (user.getPassword().equals(dbUser.getPassword())) {
				return dbUser;
			}
		}
		return null;
	}

	@Override
	public void operatePoint(int userId, OperatePoint operatePoint) {
		String hql = "UPDATE User SET point=point+? WHERE id=?";
		userDao.batchExecute(hql, operatePoint.operate(), userId);
	}

	/**
	 * 个人头像上传
	 * 
	 * @param request
	 * @param response
	 */
	@Override
	public String uploadAvatar(MultipartFile file, User user) {
		String returnPath = "";
		try {
			String folder = "/images/useravatar"
					+ new SimpleDateFormat("/yyyy/MM/").format(new Date())
					+ user.getId();
			// 删除旧文件
			File avatarOld = new File(Config.getUserAvatar() + user.getAvatar());
			if (avatarOld.exists()) {
				avatarOld.delete();
			}

			File tempAvatarDir = new File(Config.getUserAvatar() + folder);
			if (!tempAvatarDir.exists()) {
				tempAvatarDir.mkdirs();
			}
			String fileType = ".jpg";
			long currentTime = System.currentTimeMillis();
			int timestamp = (int) (currentTime / 1000);
			returnPath = folder + "/" + timestamp + fileType;
			File tempAvatar = new File(Config.getUserAvatar() + returnPath);
			if (file != null) {
				file.transferTo(tempAvatar);
			}
			this.updateAtavar(returnPath, user.getId());

		} catch (Exception e) {
			log.debug(e.getMessage());
		} finally {
			try {
				file.getInputStream().close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return returnPath;
	}

	@Override
	public int updateAtavar(String avatar, int userId) {
		String hql = "UPDATE User SET avatar=? WHERE id=?";
		return userDao.batchExecute(hql, avatar, userId);
	}
}
